package com.sdk.apiNotify.rabbitmq.consumber.handle;

import com.coment.dto.ApiNotify;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sdk.service.impl.ApiNotifyServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class ApiNotifyErrorHandle implements MessageListener {
   @Resource
   private ApiNotifyServiceImpl apiNotifyService;

   public void onMessage(Message message) {
      try {
         ObjectMapper mapper = new ObjectMapper();
         ApiNotify apiNotify = mapper.readValue(message.getBody(), ApiNotify.class);
         log.error("死信队列消息{}，最终处理失败,次数: {}", apiNotify.getMessageId(), apiNotify.getRetryCount());
         apiNotify.setStatus(2);
         this.apiNotifyService.saveApiNotify(apiNotify);
      } catch (Throwable $ex) {
          try {
              throw $ex;
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
   }
}
